package com.yhx.controller;

import com.yhx.iservice.IUserService;
import com.yhx.pojo.User;
import com.yhx.utils.MessageConstant;
import com.yhx.utils.PageResult;
import com.yhx.utils.QueryPageBean;
import com.yhx.utils.Result;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private IUserService iUserService;
    @PostMapping("/listpage")
    public PageResult listpage(@RequestBody QueryPageBean queryPageBean){
        try {
            PageResult listpage = iUserService.listpage(queryPageBean);
            return listpage;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping
    public Result save(@RequestBody User user){
        try {

            int save = iUserService.save(user);
            if (save>0){
                return new Result(true, MessageConstant.ADD_USER_SUCCESS);
            }
            return new Result(false,MessageConstant.ADD_USER_FAIL );
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_USER_FAIL );
        }

    }


    @DeleteMapping
    public Result delete(Integer[] ids){
        System.out.println(ids);
        try {
            int delte = iUserService.delete(ids);
            if(delte>0){
                return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
            }
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
    }
    @GetMapping("/{id}")
    public Result getByid(@PathVariable int id){
        try {
            User byid = iUserService.getByid(id);
            if (byid!=null){
                return new Result(true, MessageConstant.QUERY_USER_SUCCESS,byid);
            }
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_USER_FAIL);
        }
    }
    @PutMapping
    public Result upsate(@RequestBody User user){
        try {
            int update = iUserService.update(user);
            if(update>0){
                return new Result(true, MessageConstant.EDIT_USER_SUCCESS);
            }
            return new Result(false, MessageConstant.EDIT_USER_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_USER_FAIL);
        }
    }
    @DeleteMapping("/{id}")
    public Result deleteOne(@PathVariable int id){
        try {
            int i = iUserService.deleteOne(id);
            if(i>0){
                return new Result(true, MessageConstant.DELETE_USER_SUCCESS);
            }
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_USER_FAIL);
        }
    }
}
